<?php
/* 
* @Author: Jerry-chen
* @Date:   2014-07-25 01:15:03
* @Last Modified time: 2014-07-27 16:05:11
* @Last Modified by: Jerry-chen<uranusbill@hotmail.com>
* @ Copyright (c) 2014 http://www.cooldreamer.com All rightsreserved.
*/

class SearchControl extends PublicControl
{
	
	function index()
	{
		if(IS_POST)
		{
			// 分配搜索条件
			$this->assign('Search',$_POST['Search']);
			// p($_POST['Search']);
			$temp_arr = array();
			$SearchWord = String::splitWord(Q('post.Search'));
			// p($SearchWord);
			$arr_temp=array();
			foreach ($SearchWord as $k => $v) {
				$result = K('Goods')->search("goodsname like '%$k%'",array('goodsid','title','cinemaid','startime','overtime','goodsname')) ;
				// P($result);
				// $temp_arr =array_intersect($result, $temp_arr);
				
				// p($result);
				//替换成红字
				if(is_array($result))
				{
					foreach ($result as $ki => $vi) {
					//用RE_REPLACE替换成红色的字
						$result[$ki]['goodsname']=str_replace($k, '<span style="color:red">'.$k.'</span>', $vi['goodsname']);
						// p($result[$ki]);
					}
					// p($result);
					$arr_temp[]=$result;
				}
				
				// $tempAtrr[$k]['goodsname'] = str_replace(search, replace, subject)
				// p($arr_temp);
			}
			// p($arr_temp);
			// die;
			$tempAtrr = array();
			foreach ($arr_temp as $k => $v) {
				if(is_array($v))
				{
				foreach ($v as $sk => $sv) {
				$tempAtrr[]=$sv;
					}
				}
				
			}
			// p($tempAtrr);
			foreach ($tempAtrr as $k => $v) {
				 //DED详细描述---
		         $desData = K('Goodsdes')->get_in(array('goodsid'=>$v['goodsid']),'goodsid desc',array('goodsimage','goodsid'));
		         // p($desData);
				 // p($desData[$k]['goodsimage']);
		          $tempAtrr[$k]['goodsimage'] = $desData[0]['goodsimage'];
		          $place = K('Cinema')->get_one(array('cinemaid'=>$v['cinemaid']));
		          // p($place);
		          $tempAtrr[$k]['cinemaname'] = $place['cinemaname'];
		          $tempAtrr[$k]['cinemaaddress'] = $place['cinemaaddress'];
		           $city = k('Goodsattr')->get_one(array('goodsid'=>$v['goodsid']),array('city','goodsid'));
		          $tempAtrr[$k]['city'] = $city['city'];

			}
			// p($tempAtrr);
			 $this->assign('tempAtrr',$tempAtrr);
		}
		
		$this->display('Search.html');
	}
}
